package com.zjitc.service.impl;

import com.zjitc.common.ServerResponse;
import com.zjitc.dao.UserDao;
import com.zjitc.model.Cart;
import com.zjitc.model.User;
import com.zjitc.service.UserService;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/4/17 15:46
 * @description:
 */
@Service
public class UserServiceImpl implements UserService {

  @Autowired
  private UserDao userDao;

  @Override
  public User login(String username, String password) {
    int count = userDao.checkUsernameCount(username);
    /*验证用户名是否存在 若不存在 返回信息*/
    String msg = null;
    if (count != 1) {
      msg = "The Username do not Exit !";
    }
    /*若用户名存在 验证密码正确性*/
    User user = userDao.checkByUsernameAndPassword(username, password);
    /*若密码错误*/
    if (!password.equals(user.getPassword())) {
      ServerResponse serverResponse = ServerResponse.createError("The Password is Error !");
    }
    user.setPassword(null);
    user.setQuestion(null);
    user.setAnswer(null);
    /*若成功*/
    return user;
  }

  @Override
  public int checkUsernameCount(String username) {
    return userDao.checkUsernameCount(username);
  }

  @Override
  public void insertUser(String username, String password, String email, String phone,
      String question, String answer, Date create_time, Date update_time,
      Integer role) {
    userDao.insertUser(username, password, email, phone, question, answer, create_time, update_time,
        role);
  }

  @Override
  public List<Cart> cartLIst(String username) {
    return userDao.cartLIst(username);
  }

  @Override
  public void addCart(int catid, String username, String uuid) {
    /*UUID uuid = UUID.randomUUID();
    String str = uuid.toString();*/

    userDao.addCart(uuid, username, catid);
  }

  @Override
  public User findAll(String username) {
    return userDao.findAll(username);
  }

  @Override
  public void changeRole(String username) {
    userDao.changeRole(username);
  }
}
